{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca0497c2",
   "metadata": {},
   "source": [
    "# week03\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d54369d4",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d7f1324",
   "metadata": {},
   "source": [
    "### 0.人脸API的第一部，获取人脸face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e8d5f6a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入调用模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "289be564",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入你的API网站注册的API_Key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a30908d",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_Key = \"-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1d5551c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_secret = \"InL8GaEbniNEzTHg3GqUrj329rqRAU81\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4a3413b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 准备 API测试接口的url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cc98a3cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "748de9c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 调用方法post"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "90390918",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 准备url传递参数 （参数名按照官方id写）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "421d5217",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://digitalfission.fs.msgtimes.com/faceapi/asset/images/db/4.jpg\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "       \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4145d953",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.发起请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54c83b3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(url=face_detect,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0dff865a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "29bf3df9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647672212,be540a29-3f11-4259-bd17-6d606982d0f8',\n",
       " 'time_used': 588,\n",
       " 'faces': [{'face_token': 'b7f2273149cc01b0e6984fe022cfd563',\n",
       "   'face_rectangle': {'top': 377, 'left': 313, 'width': 351, 'height': 351},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 39},\n",
       "    'smile': {'value': 99.999, 'threshold': 50.0}}}],\n",
       " 'image_id': '1DbjIbz9ZarY+Mzrd5yJWA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "874acde6",
   "metadata": {},
   "source": [
    "# face 2 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "37e6a392",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c9646eb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://files.pitchbook.com/website/images/ar/main/b/2x/D_Beckham.jpg'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bc5e7fbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "26808edd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647672954,ed50ff89-5509-4f58-8110-e837274adc50',\n",
       " 'time_used': 671,\n",
       " 'faces': [{'face_token': '78398eb6e1d2e24647b4199a234c13d4',\n",
       "   'face_rectangle': {'top': 295, 'left': 402, 'width': 430, 'height': 430}}],\n",
       " 'image_id': 'A5OXAQoYD3BdOjfpxcy7yw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "955cf915",
   "metadata": {},
   "source": [
    "# 人脸分析 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9e2115b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':\"b7f2273149cc01b0e6984fe022cfd563\",\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7231fcb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "640596d8",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 198,\n",
       " 'request_id': '1647672483,67780cc8-85c0-4983-9fde-967e43ad00b6',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.0,\n",
       "     'neutral': 0.0,\n",
       "     'disgust': 0.003,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.997},\n",
       "    'beauty': {'female_score': 78.416, 'male_score': 68.769},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 39},\n",
       "    'mouthstatus': {'close': 99.954,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.0,\n",
       "     'other_occlusion': 0.046},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 16.965,\n",
       "     'stain': 2.24,\n",
       "     'acne': 3.338,\n",
       "     'health': 6.526},\n",
       "    'headpose': {'yaw_angle': 5.27662,\n",
       "     'pitch_angle': 12.543078,\n",
       "     'roll_angle': -4.0832496},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.336},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.336},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.336}},\n",
       "    'smile': {'threshold': 50.0, 'value': 99.999},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.071,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.928,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 84.461},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.503,\n",
       "      'vector_z_component': 0.993,\n",
       "      'vector_x_component': 0.013,\n",
       "      'vector_y_component': 0.118,\n",
       "      'position_y_coordinate': 0.425},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.442,\n",
       "      'vector_z_component': 0.99,\n",
       "      'vector_x_component': -0.137,\n",
       "      'vector_y_component': -0.027,\n",
       "      'position_y_coordinate': 0.423}}},\n",
       "   'face_rectangle': {'width': 351, 'top': 377, 'left': 313, 'height': 351},\n",
       "   'face_token': 'b7f2273149cc01b0e6984fe022cfd563'}]}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9512ed2",
   "metadata": {},
   "source": [
    "# 人脸对比 （贝克汉姆）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9fedd136",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':\"b7f2273149cc01b0e6984fe022cfd563\",\n",
    "    'face_token2':\"78398eb6e1d2e24647b4199a234c13d4\"\n",
    "    \n",
    "}\n",
    "r = requests.post(url = compare_url, params = payload)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "43a35c0c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "49110cf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647673365,6de62df8-8703-485b-9529-70c9660e11cf',\n",
       " 'time_used': 341,\n",
       " 'confidence': 89.623,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bc17ed0",
   "metadata": {},
   "source": [
    "# 3.封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7602994",
   "metadata": {},
   "source": [
    "##  1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ccb3831c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f5e5ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_sercret,image_url，landmark=None,attributes=None):\n",
    "    \"\"\"face++ 人脸检测\"\"\"\n",
    "    import requests\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':image_url，\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes\n",
    "    }\n",
    "\n",
    "    r = requests.post(url, params = payload,)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2f0eca7",
   "metadata": {},
   "source": [
    "# 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "75fd3a11",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "86dd127d",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb64867c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d33469b5",
   "metadata": {},
   "source": [
    "# 宿舍人脸识别系统"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a6e9e43",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "b4f08bda",
   "metadata": {},
   "source": [
    "### 作业\n",
    "* 1.尝试利用python函数的方法，封装 face++ 人脸检测，人脸分析 和人脸对比\n",
    "* 2.尝试调用face++ 人脸搜索API接口\n",
    "* 3.尝试应用 上述 2 ，设计宿舍人脸识别系统小应用（微信小程序尝试）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d0e154d",
   "metadata": {},
   "source": [
    "# 创建人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c363bdc6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸数据库\n",
    "import requests\n",
    "\n",
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0526d43f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9ff2f971",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '62c4653d392a3041ebb082687ffc52d6',\n",
       " 'time_used': 153,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647915277,daf2e553-8de8-4ebc-a19d-f5f8b00bfb97',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "218e134e",
   "metadata": {},
   "source": [
    "# 查询人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31e7a9ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = '-sktIe7XLrdVWO6YPccL5lz1RUb5-gQQ'\n",
    "API_sercret = 'InL8GaEbniNEzTHg3GqUrj329rqRAU81'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "68ab876d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'facesets'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-16fa66399352>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'facesets'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'facesets'"
     ]
    }
   ],
   "source": [
    "r.json()['facesets'][-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e3e994e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
